/*
 * DS1820.cpp
 *
 *  Created on: May 14, 2009
 *      Author: David
 */

#include <WProgram.h>
#include "DS1820.h"

/*
 * DS1820.c
 *
 *  Created on: May 14, 2009
 *      Author: David
 */

#include "OneWire.h"
#include <stdio.h>
#include <avr/io.h>

#define CONVERT     0x44
#define READSCRATCH 0xBE
#define SKIP_ROM    0xCC
#define MATCH_ROM   0x55
#define END_FLAG    -9999         //shows how many sensors we found
#define BANK_TANK      0
#define BANK_COLLECTOR 1
#define LED_PIN          13  // flash an led on 13 - we all like flashing lights

OneWire ds(TEMPERATURE_PIN);

int numberOfTemperatureSensors=0;
extern char* tempBuf;


void temperatureSetup()
  {
  pinMode(TEMPERATURE_PIN,OUTPUT);  //Charge the one-wire sensor in parasite mode
  digitalWrite(TEMPERATURE_PIN,HIGH);
  }

//Sensor number is 1-based, not zero-based.
boolean hasTemperatureSensor(int sensorNumber)
  {
  return sensorNumber>0 && sensorNumber<=numberOfTemperatureSensors;
  }

uint8_t doSearch(byte* addr)
	{
	uint8_t answer;
	noInterrupts();
	answer=ds.search(addr);
	interrupts();
	return answer;
	}

void listDevices()
  {
  byte addr[8];
  ds.reset_search();
  while(doSearch(addr))
    {
    digitalWrite(LED_PIN, HIGH);   // sets the LED on
    if ( OneWire::crc8( addr, 7) == addr[7])
      {
       if ( addr[0] == 0x10)  // Make sure it is a DS18S20 device
         {
         char buffer[30];
         sprintf(buffer,"1-Wire ID:%02x%02x%02x%02x%02x%02x%02x%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
         Serial.println(buffer);
         }
      }
    }
  }

void tempToAscii(double temp, char *buff)
  {
  int frac;
  frac=(unsigned int)(temp*1000)%1000;  //get three numbers to the right of the deciaml point
  itoa((int)temp,buff,10);
  strcat(buff,".");
  itoa(frac,&buff[strlen(buff)],10); //put the frac after the deciaml
  }

//Read the temperature in Celsius from a sensor directly. Use *1.8+32 to
//convert to Farenheit.
float readTemperature(uint8_t* sensorAddress)
  {
  byte data[12];
  float read_temp;
  float real_temp;
  float temp_count;

  noInterrupts(); //have to do this here, the OneWire source is bad
  ds.reset();              // Reset device
  ds.select(sensorAddress);// Select device
  ds.write(CONVERT,1);     // Issue command to read the temperature
  interrupts();
  delay(1000);             // maybe 750ms is enough, maybe not

  noInterrupts();
  ds.reset();               // Reset device
  ds.select(sensorAddress); // Select device
  ds.write(READSCRATCH);    // Read Scratchpad

  for (int i = 0; i < 9; i++) // we need 9 bytes
    {
    data[i] = ds.read();
    }
  interrupts();

  if(OneWire::crc8( data, 8) == data[8])  // Check CRC is valid
    {
    // CRC is ok
    read_temp=((data[1]<<8) | data[0]) >> 1 ; // Divide the temperature by 2
    temp_count=float(data[7] - data[6])/(float)data[7];// Convert to real temperature
    real_temp = ((float)read_temp-0.25)+temp_count;
    }
  else
    {  //CRC failed
//    Serial.println("CRC Failed");
    real_temp=-200;
    }
  return real_temp;
  }
